package com.wing.portal.aggregation.portletinfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.wing.cms.domain.Portlet;
import com.wing.cms.domain.Portlet2;
import com.wing.cms.service.Portlet2Manager;
import com.wing.common.util.PageBean;
import com.wing.common.util.SpringDI;
import com.wing.ext.domain.Products;
import com.wing.ext.service.ProductgroupManager;
import com.wing.ext.service.ProductsManager;

@Service
public class ProductPortletInfo implements PortletInfo {
	
	private List<Map<String, Object>> findProductgrpsByPid(String pid ,String siteId) {
		ProductgroupManager productgroupManager = (ProductgroupManager) SpringDI
				.getBean("productgroupManager");
		List<Map<String, Object>> artgroups = productgroupManager.findChildByID(pid, siteId);
		for (Map<String, Object> artgroup : artgroups) {
			setChild(productgroupManager,artgroup,siteId);
		}
		return artgroups;
	}

	private void setChild(ProductgroupManager productgroupManager,Map<String, Object> artgroup,String siteId) {
		String id = (String)artgroup.get("id");
		List<Map<String, Object>> artgroups = productgroupManager.findChildByID(id, siteId);
		for (Map<String, Object> artg : artgroups) {
			setChild(productgroupManager,artg,siteId);
		}
		artgroup.put("childList", artgroups);
	}
	
	/**
	 * 通过portletId查找模板位置
	 * 
	 * @param portletId 
	 * @return templateFilePath
	 */

	public String findTemplateFile(String portletId) {
		Portlet2Manager portletInstance1Manager = (Portlet2Manager) SpringDI
				.getBean("portlet2Manager");
		return portletInstance1Manager.findTemplateFile(portletId);
	}

	public Map loadPortletInfo(Map<String, String> param) {
		Map root = new HashMap();
		String portletId = param.get("portletid");
		String siteId = param.get("siteId");
		Portlet2Manager portletInstanceManager = (Portlet2Manager) SpringDI
				.getBean("portlet2Manager");
		ProductsManager productsManager = (ProductsManager) SpringDI
		.getBean("productsManager");
		
		Portlet2 portletInfo = portletInstanceManager.findById(portletId);
		if(portletInfo==null){
			root.put("portletWidth", 40);
			root.put("portletheight", 10);
			return root;
		}
		PageBean productList = productsManager.findPageList(1, portletInfo.getProductcount(), portletInfo.getProductgroupid());
		root.put("groupid", portletInfo.getProductgroupid());
		root.put("title", portletInfo.getTitle());
		root.put("titlecolor", portletInfo.getTitlecolor());
		root.put("titletype", portletInfo.getTitletype()==null?"0":portletInfo.getTitletype());
		root.put("titlepic", portletInfo.getTitlepic());
		root.put("portletWidth", portletInfo.getPortletwidth());
		root.put("portletheight", portletInfo.getPortletheight());
		root.put("assoportlet", portletInfo.getAssoportlet());
		String isnavi = portletInfo.getIsnavi();
		List<Products> showProductList = new ArrayList();
		if(Portlet.ISNAVI_TRUE.equals(isnavi)){
			List<Map<String, Object>> productgrps = findProductgrpsByPid(portletInfo.getProductgroupid(),siteId);
			root.put("productgrps",productgrps);
		}else{
			for (int i = 0; i < productList.getRecordList().size(); i++) {
				Products tempProduct = new Products();
				Map<String, Object> dataMap = productList.getRecordList().get(i);
				tempProduct.setId((String) dataMap.get("id"));
				// 设置展示题目
				String title = (String) dataMap.get("product_name");
				double titleLengthFlag = 0;
				StringBuffer sb = new StringBuffer();
				String showTitle = title;
				if (title.getBytes().length > portletInfo.getTitlelength() * 2) {
					for (int a = 0; a < title.length(); a++) {
						char c = title.charAt(a);
						if (String.valueOf(c).getBytes().length == 2) {
							titleLengthFlag += 1;
						} else {
							titleLengthFlag += 0.5;
						}
						sb.append(c);
						if (titleLengthFlag >= portletInfo.getTitlelength())
							break;
					}
					showTitle = sb.toString() + "...";
				}
				tempProduct.setProduct_name(showTitle);
				// 设置产品介绍
				String introduction = (String) dataMap.get("introduction");
				double introductionLengthFlag = 0;
				StringBuffer sbjs = new StringBuffer();
				String showintroduction = introduction;
				if (introduction.getBytes().length > portletInfo.getSummarylength() * 2) {
					for (int a = 0; a < introduction.length(); a++) {
						char c = introduction.charAt(a);
						if (String.valueOf(c).getBytes().length == 2) {
							introductionLengthFlag += 1;
						} else {
							introductionLengthFlag += 0.5;
						}
						sbjs.append(c);
						if (introductionLengthFlag >= portletInfo.getSummarylength())
							break;
					}
					showintroduction = sbjs.toString() + "...";
				}
				tempProduct.setPromotion(showintroduction);
				tempProduct.setCreate_user((String) dataMap.get("create_user"));
				tempProduct.setGroupid((String) dataMap.get("groupid"));
				tempProduct.setInventory((Integer)dataMap.get("inventory"));
				tempProduct.setKeyword((String) dataMap.get("keyword"));
				tempProduct.setPicpath((String) dataMap.get("picpath"));
				tempProduct.setSmall_picpath((String) dataMap.get("small_picpath"));
				tempProduct.setPrice((Integer)dataMap.get("price"));
				tempProduct.setProduct_code((String) dataMap.get("product_code"));
				showProductList.add(tempProduct);
			}
		}
		
		if (productList.getRecordList().size() == 0) {
			root.put("haveProducts", "none");
		} else {
			root.put("haveProducts", "many");
//			Products[] products = new Products[showProductList.size()];
//			showProductList.toArray(products);
			root.put("products", showProductList);
		}
		return root;
	}
	
	
}
